data "alicloud_zones" "default" {
  available_resource_creation = "Rds"
}

data "alicloud_db_zones" "zones_ids" {
  engine                   = var.engine
  engine_version           = var.engine_version
  multi_zone               = var.multi_zone
  db_instance_storage_type = var.instance_storage_type
}

locals {
  zone_ids        = data.alicloud_db_zones.zones_ids.zones.0.multi_zone_ids
  zone_id         = local.zone_ids[0]
  zone_id_slave_a = local.zone_ids[1]

  vswitch_0 = element(var.exist_vswitch_ids, index(var.exist_azs, local.zone_id))
  vswitch_1 = element(var.exist_vswitch_ids, index(var.exist_azs, local.zone_id_slave_a))

  vswitch_id = join(",", [local.vswitch_0, local.vswitch_1])
}

module "rds" {
  source  = "git@gitee.com:mmbluex/terraform-alicloud-rds.git"

  zone_id                    = local.zone_id
  zone_id_slave_a            = local.zone_id_slave_a
  zone_id_slave_b            = var.zone_id_slave_b
  vswitch_id                 = local.vswitch_id

  instance_name              = var.instance_name
  instance_type              = var.instance_type
  instance_storage_type      = var.instance_storage_type
  instance_storage           = var.instance_storage
  security_ips               = var.security_ips

  parameters                 = var.parameters
  allocate_public_connection = var.allocate_public_connection
  databases                  = var.databases
  accounts                   = var.accounts
  tags                       = var.tags
}